clear
clear mata
clear matrix

capture log close
log using "APST_replication figures.log", replace

******************************************************************************

*       Replication file for "Agricultural Productivity and Structural Transformation"
*               by Paula Bustos, Bruno Caprettini and Jacopo Ponticelli

*       FIRST VERSION   March      31, 2015
*       THIS VERSION    November   22, 2015


*       LAST REVISOR		BC
		
*	    This file reproduces and exports all figures produced for the paper:
*               "Agricultural Productivity and Structural Transformation"
*       Forthcoming on the American Economic Review.

*       BEWARE: in case they are not already installed, this do-file automatically 
*               downloads 2 user-written programs (shp2dta.ado and spmap.ado). They 
*               are needed to produce the maps in figures 2, 3 and A7.
*******************************************************************************


****************************************************************************************************
****                                   PLAN OF THE PROCEDURE                                    ****
****��������������������������������������������������������������������������������������������****
****                             Figures in the body of the paper                               ****
****��������������������������������������������������������������������������������������������****
**** Figure 1: Soy and Maize in Brazil 1980-2010                                                ****
**** Figure 2: Technological change in soy: municipalities (AMC)                                ****
**** Figure 3: Technological change in soy: micro-regions                                       ****
****                                                                                            ****
****                             Figures in the appendix                                        ****
****��������������������������������������������������������������������������������������������****
**** Figure A1: Employment in agriculture, industry and services                                ****
**** Figure A3: Suitability to soy cultivation: low inputs                                      ****
**** Figure A4: Suitability to soy cultivation: high inputs                                     ****
**** Figure A5: Suitability to maize cultivation: low inputs                                    ****
**** Figure A6: Suitability to maize cultivation: high inputs                                   ****
**** Figure A7: Agricultural frontier                                                           ****
**** Figure A8: Real price of soy                                                               ****
**** Figure A9: Real price of maize                                                             ****
****************************************************************************************************

****************************************************************************************************
**** Figure 1: Soy and Maize in Brazil 1980-2010                                                ****
****************************************************************************************************
use APST_figures, replace
	
twoway (scatter soy_Q_L t if year>=1980 & year<=1990, lpattern(solid) 	connect(l) lcolor(black) mcolor(black) msize(medium) mstyle(circle) ) ///
	   (scatter soy_Q_L t if year>=1990 & year<=1992, lpattern(dash) 	connect(l) lcolor(black) mcolor(black) msize(medium) mstyle(circle) )  ///
	   (scatter soy_Q_L t if year>=1992 & year<=1993, lpattern(solid) 	connect(l) lcolor(black) mcolor(black) msize(medium) mstyle(circle) )  ///
	   (scatter soy_Q_L t if year>=1993 & year<=1995, lpattern(dash) 	connect(l) lcolor(black) mcolor(black) msize(medium) mstyle(circle) )  ///
	   (scatter soy_Q_L t if year>=1995 & year<=1999, lpattern(solid) 	connect(l) lcolor(black) mcolor(black) msize(medium) mstyle(circle) )  ///
	   (scatter soy_Q_L t if year>=1999 & year<=2001, lpattern(dash) 	connect(l) lcolor(black) mcolor(black) msize(medium) mstyle(circle) )  ///
	   (scatter soy_Q_L t if year>=2001 & year<=2009, lpattern(solid) 	connect(l) lcolor(black) mcolor(black) msize(medium) mstyle(circle) )  /// 
	   (scatter soy_Q_L t if year>=2009 & year<=2011, lpattern(dash) 	connect(l) lcolor(black) mcolor(black) msize(medium) mstyle(circle) )  ///
                     , scheme(s1manual) xlabel(3(10)33, valuelabel) ylabel(, grid) legend(off)  xtitle("Year") ytitle("tones per worker")
graph export Figures\Figure1_PanelA.pdf, replace as(pdf)
					 
twoway (scatter soy_T t if year > 1979, lpattern(solid) lcolor(black) msize(medium) mcolor(black) mstyle(circle) connect(l) scheme(s1manual)) ///
                     , xlabel(3(10)33, valuelabel) legend(off)  ytitle("area (million ha)") xtitle("Year") ylabel(, grid)
graph export Figures\Figure1_PanelB.pdf, replace as(pdf)

twoway (scatter soy_T_L t if year>=1980 & year<=1990, lpattern(solid) 	connect(l) lcolor(black) mcolor(black) msize(medium) mstyle(circle) ) ///
       (scatter soy_T_L t if year>=1990 & year<=1992, lpattern(dash) 	connect(l) lcolor(black) mcolor(black) msize(medium) mstyle(circle) )  ///
       (scatter soy_T_L t if year>=1992 & year<=1993, lpattern(solid) 	connect(l) lcolor(black) mcolor(black) msize(medium) mstyle(circle) )  ///
       (scatter soy_T_L t if year>=1993 & year<=1995, lpattern(dash) 	connect(l) lcolor(black) mcolor(black) msize(medium) mstyle(circle) )  ///
       (scatter soy_T_L t if year>=1995 & year<=1999, lpattern(solid) 	connect(l) lcolor(black) mcolor(black) msize(medium) mstyle(circle) )  ///
       (scatter soy_T_L t if year>=1999 & year<=2001, lpattern(dash) 	connect(l) lcolor(black) mcolor(black) msize(medium) mstyle(circle) )  ///
       (scatter soy_T_L t if year>=2001 & year<=2009, lpattern(solid) 	connect(l) lcolor(black) mcolor(black) msize(medium) mstyle(circle) )  /// 
       (scatter soy_T_L t if year>=2009 & year<=2011, lpattern(dash) 	connect(l) lcolor(black) mcolor(black) msize(medium) mstyle(circle) )  ///
	                 , scheme(s1manual) xlabel(3(10)33, valuelabel) ylabel(0(25)100, grid) legend(off)  xtitle("Year") ytitle("hectares per worker")
graph export Figures\Figure1_PanelC.pdf, replace as(pdf)

twoway (scatter soy_L   t if year>=1980 & year<=1990, lpattern(solid) 	connect(l) lcolor(black) mcolor(black) msize(medium) mstyle(circle) ) ///
       (scatter soy_L   t if year>=1990 & year<=1992, lpattern(dash) 	connect(l) lcolor(black) mcolor(black) msize(medium) mstyle(circle) )  ///
       (scatter soy_L   t if year>=1992 & year<=1993, lpattern(solid) 	connect(l) lcolor(black) mcolor(black) msize(medium) mstyle(circle) )  ///
       (scatter soy_L   t if year>=1993 & year<=1995, lpattern(dash) 	connect(l) lcolor(black) mcolor(black) msize(medium) mstyle(circle) )  ///
       (scatter soy_L   t if year>=1995 & year<=1999, lpattern(solid) 	connect(l) lcolor(black) mcolor(black) msize(medium) mstyle(circle) )  ///
       (scatter soy_L   t if year>=1999 & year<=2001, lpattern(dash) 	connect(l) lcolor(black) mcolor(black) msize(medium) mstyle(circle) )  ///
       (scatter soy_L   t if year>=2001 & year<=2009, lpattern(solid) 	connect(l) lcolor(black) mcolor(black) msize(medium) mstyle(circle) )  /// 
       (scatter soy_L   t if year>=2009 & year<=2011, lpattern(dash) 	connect(l) lcolor(black) mcolor(black) msize(medium) mstyle(circle) )  ///
	                 , scheme(s1manual) xlabel(3(10)33, valuelabel) ylabel(0(175)700, grid) legend(off) xtitle("Year") ytitle("workers (in thousands)")
graph export Figures\Figure1_PanelD.pdf, replace as(pdf)
	
twoway (scatter mze_T   t if year>=1980 & year<=2011, lpattern(solid)   connect(l) lcolor(black) mcolor(black) msize(medium) msymbol(O))      ///
       (scatter mz1_T   t if year>=1980 & year<=2011, lpattern(solid)   connect(l) lcolor(black) mcolor(black) msize(medium) msymbol(T))      ///
	   (scatter mz2_T   t if year>=1980 & year<=2011, lpattern(solid)   connect(l) lcolor(black) mcolor(black) msize(medium) msymbol(+))      ///
                     , scheme(s1manual) xlabel(3(10)33, valuelabel) ylabel(0[5]19, grid) ytitle("area (million ha)") xtitle("Year")  legend(label(1 "Total") label(2 "1st season") label(3 "2nd season"))
graph export Figures\Figure1_PanelE.pdf, replace as(pdf)



****************************************************************************************************
**** Figure 2: Technological change in soy: municipalities (AMC)                                ****
****************************************************************************************************
capture ssc install shp2dta      /* this command is needed to convert shp into dta  */
capture ssc install spmap        /* this command is needed to plot the map in stata */

shp2dta using Maps\Brazil_AMC        , data(Maps\Brazil_AMC        ) coord(Maps\C-Brazil_AMC        ) replace

use APST_AMC, replace
merge m:1 AMC using Maps\Brazil_AMC, keep(mas mat) keepus(_ID)

keep if year == 2010 & _ID != .
format dA_soy %2.1f
spmap dA_soy using Maps\C-Brazil_AMC, id(_ID) ndsize(.0000000000000000000000000001) osize(.0000000000000000000000000001) mosize(.0000000000000000000000000001) clmethod(quantile) clnumber(11) fcolor(RdYlGn) 
graph export Figures\Figure2.pdf, replace as(pdf)

****************************************************************************************************
**** Figure 3: Technological change in soy: micro-regions                                       ****
****************************************************************************************************
shp2dta using Maps\Brazil_microregion, data(Maps\Brazil_microregion) coord(Maps\C-Brazil_microregion) replace

use APST_micro, replace
merge m:1 micro using Maps\Brazil_microregion, keep(mas mat) keepus(_ID)

keep if year == 2010 & _ID != .
format dA_soy %2.1f
spmap dA_soy using Maps\C-Brazil_microregion, id(_ID) clmethod(quantile) clnumber(11) fcolor(RdYlGn)  ndsize(.0000000000000000000000000001) osize(.0000000000000000000000000001) mosize(.0000000000000000000000000001)
graph export Figures\Figure3.pdf, replace as(pdf)


****************************************************************************************************
**** Figure A1: Employment in agriculture, industry and services                                ****
****************************************************************************************************
use APST_figures, replace

twoway (scatter La  year if year>1990 & year < 2010,  lpattern(solid) 	connect(l) 	lcolor(black) mcolor(black) msize(medium) msymbol(circle)   legend(label(1 "agriculture"))    scheme(s1manual) ylabel(, grid) ytitle("workers (in millions)")) ///
	   (scatter Lm  year if year>1990 & year < 2010,  lpattern(solid) 	connect(l)  lcolor(black) mcolor(black) msize(medium) msymbol(triangle) legend(label(2 "manufacturing")))           /// 
	   (scatter Ls  year if year>1990 & year < 2010,  lpattern(solid) 	connect(l)  lcolor(black) mcolor(black) msize(medium) msymbol(X)        legend(label(3 "services")))     , xscale(range(1990 2010)) xlabel(1990(5)2010) yscale(range(0(10)50))
graph export Figures\FigureA1.pdf, replace as(pdf)


****************************************************************************************************
**** Figure A3: Suitability to soy cultivation: low inputs                                      ****
**** Figure A4: Suitability to soy cultivation: high inputs                                     ****
**** Figure A5: Suitability to maize cultivation: low inputs                                    ****
**** Figure A6: Suitability to maize cultivation: high inputs                                   ****
****************************************************************************************************
* These figures were downloaded directly from the FAO-GAEZ website on December 2012
* In November 2015 the FAO website does not offer the option to download soil suitability by country.
* In order to obtain a raster-map of the *whole world* (not just Brazil) with soil suitability for soy 
* and maize under low and high input, go to: http://gaez.fao.org/Main.html 

****************************************************************************************************
**** Figure A7: Agricultural frontier                                                           ****
****************************************************************************************************
use APST_AMC, replace
merge m:1 AMC using Maps\Brazil_AMC, keep(mas mat) keepus(_ID)

keep if year == 2010 & _ID != .
spmap frontier using Maps\C-Brazil_AMC, id(_ID) clmethod(unique) clnumber(2) fcolor(RdYlGn) osize(.0000000000000000000000000001) mosize(.0000000000000000000000000001)
graph export Figures\FigureA7.pdf, replace as(pdf)

****************************************************************************************************
**** Figure A8: Real price of soy                                                               ****
****************************************************************************************************
use APST_prices, replace

line PSOYB_USD_2000 	month if year>=1990, scheme(s1manual) lcolor(black) xlabel(360(24)641, valuelabel labsize(small) angle(forty_five)) xtitle("time") ytitle("Soybeans, 2000 US$ per metric ton")
graph export Figures\FigureA8.pdf, replace as(pdf)

****************************************************************************************************
**** Figure A9: Real price of maize                                                             ****
****************************************************************************************************
line PMAIZMT_USD_2000 	month if year>=1990, scheme(s1manual) lcolor(black) xlabel(360(24)641, valuelabel labsize(small) angle(forty_five)) xtitle("time") ytitle("Maize, 2000 US$ per metric ton")
graph export Figures\FigureA9.pdf, replace as(pdf)

log close
exit
